﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace SharedLib.XmlDataModel
{
    /// <summary>
    /// XmlDataModel that implements the standard IXmlSerializable interface
    /// <para>Data objects of this class could be converted from/to Xml by standard .net XmlSerializer</para>
    /// <seealso cref="XmlDataModel"/>
    /// <seealso cref="IXmlSerializable"/>
    /// </summary>
    [Serializable]
    public abstract class SerializableXmlDataModel : XmlDataModel, IXmlSerializable
    {
        #region Constructors
        protected SerializableXmlDataModel():base(){}
        protected SerializableXmlDataModel(XmlNode node) : base(node) { }
        #endregion

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            LoadXmlNode(doc);
        }

        public void WriteXml(XmlWriter writer)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode output = ToXmlNode(doc);
            foreach (XmlAttribute attr in output.Attributes)
            {
                writer.WriteAttributeString(attr.Name, attr.Value);
            }
            output.WriteContentTo(writer);
        }
        #endregion
    }
}
